package org.swim.core.entities;

// Generated Dec 7, 2012 12:57:21 AM by Hibernate Tools 3.4.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.swim.remote.entities.ICollaboration;
import org.swim.remote.entities.IFeedback;
import org.swim.remote.entities.IMessage;
import org.swim.remote.entities.IUser;

/**
 * Collaboration generated by hbm2java
 */
@Entity
@Table(name = "Collaboration", catalog = "jboss")
public class Collaboration extends SwimEntity implements java.io.Serializable,
		ICollaboration {

	private static final long serialVersionUID = 1L;

	private Integer id;
	private User userBySenderId;
	private User userByReceiverId;
	private String title;
	private Status status = Status.REQUESTED;
	private Visibility senderVisibility = Visibility.VISIBLE;

	private Set<Message> messages = new LinkedHashSet<Message>();
	private Set<Feedback> feedbacks = new HashSet<Feedback>(0);

	public Collaboration() {
	}

	public Collaboration(Status status) {
		this.status = status;
	}

	public Collaboration(User userBySenderId, User userByReceiverId,
			String title, Status status, Set<Message> messages,
			Set<Feedback> feedbacks) {
		this.userBySenderId = userBySenderId;
		this.userByReceiverId = userByReceiverId;
		this.title = title;
		this.status = status;
		this.messages = messages;
		this.feedbacks = feedbacks;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getId()
	 */
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getUserBySenderId()
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "senderId")
	public User getUserBySenderId() {
		return this.userBySenderId;
	}

	public void setUserBySenderId(User userBySenderId) {
		this.userBySenderId = userBySenderId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getUserByReceiverId()
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "receiverId")
	public User getUserByReceiverId() {
		return this.userByReceiverId;
	}

	public void setUserByReceiverId(User userByReceiverId) {
		this.userByReceiverId = userByReceiverId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getTitle()
	 */
	@Column(name = "title", length = 45)
	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getStatus()
	 */
	@Enumerated(EnumType.STRING)
	@Column(name = "status", nullable = false, length = 10, columnDefinition = "ENUM('REQUESTED', 'CONFIRMED', 'REJECTED', 'COMPLETED')")
	public Status getStatus() {
		return this.status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	@Enumerated(EnumType.STRING)
	@Column(name = "senderVisibility", nullable = false, length = 10, columnDefinition = "ENUM('HIDDEN', 'VISIBLE')")
	public Visibility getSenderVisibility() {
		return senderVisibility;
	}

	public void setSenderVisibility(Visibility senderVisibility) {
		this.senderVisibility = senderVisibility;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.swim.entities.ICollaboration#getMessages()
	 */
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "collaboration")
	@OrderBy("timestamp DESC")
	public Set<Message> getMessages() {
		return this.messages;
	}

	public void setMessages(Set<Message> messages) {
		this.messages = messages;
	}

	public void setSingleMessage(Message message) {
		messages.add(message);
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "collaboration")
	public Set<Feedback> getFeedbacks() {
		return this.feedbacks;
	}

	public void setFeedbacks(Set<Feedback> feedbacks) {
		this.feedbacks = feedbacks;
	}

	public void setSingleFeedback(Feedback feedback) {
		feedbacks.add(feedback);
	}

	/* --------------------All method for interfaces------------------------ */
	@Transient
	public Set<IFeedback> getFeedbacksSet() {
		Set<IFeedback> set = new HashSet<IFeedback>(getFeedbacks());
		return set;
	}

	@Transient
	public List<IMessage> getMessagesList() {
		Set<IMessage> mexs = new LinkedHashSet<IMessage>(getMessages());
		List<IMessage> list = new ArrayList<IMessage>(mexs);
		return list;
	}

	@Transient
	public IUser getIUserBySenderId() {
		IUser user = getUserBySenderId();

		// if (getSenderVisibility() == Visibility.HIDDEN)
		// if (user != null && user.equals(getUserBySenderId()))
		// user = null;

		return user;
	}

	@Transient
	public boolean isMy(IUser user) {
		IUser sender = getUserBySenderId();
		IUser receiver = getUserByReceiverId();

		if ((sender != null && sender.equals(user))
				|| (receiver != null && receiver.equals(user)))
			return true;

		return false;

	}

}
